package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Bannerclient;

public class GenericBannerClientDAO extends AutoKeys implements
		org.jetnuke.dao.BannerClientDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBannerClientDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerClientModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerClientModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Bannerclient u = new Bannerclient();
			fillBannerClientFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	private void fillBannerClientFromResultSet(Bannerclient u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBannerClientFromResultSet(Bannerclient, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setCID(rs.getInt("CID"));
		u.setCONTACT(rs.getString("CONTACT"));
		u.setEMAIL(rs.getString("EMAIL"));
		u.setEXTRAINFO(rs.getString("EXTRAINFO"));
		u.setLOGIN(rs.getString("LOGIN"));
		u.setNAME(rs.getString("NAME"));
		u.setPASSWD(rs.getString("PASSWD"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBannerClientFromResultSet(Bannerclient, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerClientModel#countActiveForClient(int)
	 */
	public int countActiveForClient(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countActiveForClient(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BannerClientModel.countForClient"));
		p.setInt(1, cid);
		p.setInt(2, 1);// Active
		ResultSet rs = p.executeQuery();
		rs.next();
		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countActiveForClient(int) - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerClientModel#countInactiveForClient(int)
	 */
	public int countInactiveForClient(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countInactiveForClient(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BannerClientModel.countForClient"));
		p.setInt(1, cid);
		p.setInt(2, 0);// Inactive
		ResultSet rs = p.executeQuery();
		rs.next();
		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countInactiveForClient(int) - end"); //$NON-NLS-1$
		}
		return r;
	}

	public int addNew(Bannerclient bn) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Bannerclient) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("BannerClientModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("BannerClientModel.lastGeneratedBannerClientID"));
		initNewBannerClient(bn, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Bannerclient) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param bn
	 * @param p
	 */
	private void initNewBannerClient(Bannerclient bn, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewBannerClient(Bannerclient, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, bn.getCONTACT());
		p.setString(2, bn.getEMAIL());
		p.setString(3, bn.getEXTRAINFO());
		p.setString(4, bn.getLOGIN());
		p.setString(5, bn.getNAME());
		p.setString(6, bn.getPASSWD());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewBannerClient(Bannerclient, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerClientModel#deleteByID(int)
	 */
	public void deleteByID(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerClientModel.deleteByID"));
		p.setInt(1, cid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerClientModel#retrieveByPK(int)
	 */
	public Bannerclient retrieveByPK(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerClientModel.selectByID"));
		p.setInt(1, cid);

		ResultSet rs = p.executeQuery();
		Bannerclient s = new Bannerclient();

		if (rs.next()) {
			fillBannerClientFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BannerClientModel#update(org.jetnuke.entities.Bannerclient)
	 */
	public void update(Bannerclient client) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Bannerclient) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BannerClientModel.update"));
		p.setString(1, client.getCONTACT());
		p.setString(2, client.getEMAIL());
		p.setString(3, client.getEXTRAINFO());
		p.setString(4, client.getLOGIN());
		p.setString(5, client.getNAME());
		p.setString(6, client.getPASSWD());
		p.setInt(7, client.getCID());
		p.executeUpdate();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Bannerclient) - end"); //$NON-NLS-1$
		}
	}
}
